// RegistryUtils.cpp

#include "StdAfx.h"

#include "Registry.h"

#include "RegistryUtils.h"


#define REG_PATH_7Z _T("Software") STRING_PATH_SEPARATOR _T("7-Zip")

namespace Common {
namespace Registry {

static const TCHAR *kCUBasePath = REG_PATH_7Z;
static const TCHAR *kCU_FMPath = REG_PATH_7Z STRING_PATH_SEPARATOR _T("FM");
// static const TCHAR *kLM_Path = REG_PATH_7Z TEXT(STRING_PATH_SEPARATOR) TEXT("FM");

static const TCHAR *kLangValueName = _T("Lang");
static const TCHAR *kEditor = _T("Editor");
static const TCHAR *kDiff = _T("Diff");
static const TCHAR *kShowDots = _T("ShowDots");
static const TCHAR *kShowRealFileIcons = _T("ShowRealFileIcons");
static const TCHAR *kShowSystemMenu = _T("ShowSystemMenu");

static const TCHAR *kFullRow = _T("FullRow");
static const TCHAR *kShowGrid = _T("ShowGrid");
static const TCHAR *kAlternativeSelection = _T("AlternativeSelection");
// static const TCHAR *kLockMemoryAdd = _T("LockMemoryAdd");
static const TCHAR *kLargePagesEnable = _T("LargePages");
static const TCHAR *kSingleClick = _T("SingleClick");
// static const TCHAR *kUnderline = TEXT("Underline");

static const TCHAR *kFlatViewName = _T("FlatViewArc");

static void SaveCuString(LPCTSTR keyPath, LPCWSTR valuePath, LPCWSTR value)
{
	Common::Registry::CKey key;
	key.Create(HKEY_CURRENT_USER, keyPath);
	key.SetValue(valuePath, value);
}

static void ReadCuString(LPCTSTR keyPath, LPCWSTR valuePath, CString &res)
{
  res.Empty();
  Common::Registry::CKey key;
  if (key.Open(HKEY_CURRENT_USER, keyPath, KEY_READ) == ERROR_SUCCESS)
    key.QueryValue(valuePath, res);
}

void SaveRegLang(const CString &path) { SaveCuString(kCUBasePath, kLangValueName, path); }
void ReadRegLang(CString &path) { ReadCuString(kCUBasePath, kLangValueName, path); }

void SaveRegEditor(const CString &path) { SaveCuString(kCU_FMPath, kEditor, path); }
void ReadRegEditor(CString &path) { ReadCuString(kCU_FMPath, kEditor, path); }

void SaveRegDiff(const CString &path) { SaveCuString(kCU_FMPath, kDiff, path); }
void ReadRegDiff(CString &path) { ReadCuString(kCU_FMPath, kDiff, path); }

static void Save7ZipOption(const TCHAR *value, BOOL enabled)
{
  Common::Registry::CKey key;
  key.Create(HKEY_CURRENT_USER, kCUBasePath);
  key.SetValue(value, enabled);
}

static void SaveOption(const TCHAR *value, BOOL enabled)
{
  Common::Registry::CKey key;
  key.Create(HKEY_CURRENT_USER, kCU_FMPath);
  key.SetValue(value, enabled);
}

static bool Read7ZipOption(const TCHAR *value, bool defaultValue)
{
  Common::Registry::CKey key;
  if (key.Open(HKEY_CURRENT_USER, kCUBasePath, KEY_READ) == ERROR_SUCCESS)
  {
    bool enabled;
    if (key.QueryValue(value, enabled) == ERROR_SUCCESS)
      return enabled;
  }
  return defaultValue;
}

static bool ReadOption(const TCHAR *value, bool defaultValue)
{
  Common::Registry::CKey key;
  if (key.Open(HKEY_CURRENT_USER, kCU_FMPath, KEY_READ) == ERROR_SUCCESS)
  {
    bool enabled;
    if (key.QueryValue(value, enabled) == ERROR_SUCCESS)
      return enabled;
  }
  return defaultValue;
}

/*
static void SaveLmOption(const TCHAR *value, bool enabled)
{
  CKey key;
  key.Create(HKEY_LOCAL_MACHINE, kLM_Path);
  key.SetValue(value, enabled);
}

static bool ReadLmOption(const TCHAR *value, bool defaultValue)
{
  CKey key;
  if (key.Open(HKEY_LOCAL_MACHINE, kLM_Path, KEY_READ) == ERROR_SUCCESS)
  {
    bool enabled;
    if (key.QueryValue(value, enabled) == ERROR_SUCCESS)
      return enabled;
  }
  return defaultValue;
}
*/

void SaveShowDots(BOOL showDots) { SaveOption(kShowDots, showDots); }
bool ReadShowDots() { return ReadOption(kShowDots, false); }

void SaveShowRealFileIcons(BOOL show)  { SaveOption(kShowRealFileIcons, show); }
bool ReadShowRealFileIcons() { return ReadOption(kShowRealFileIcons, false); }

void SaveShowSystemMenu(BOOL show) { SaveOption(kShowSystemMenu, show); }
bool ReadShowSystemMenu(){ return ReadOption(kShowSystemMenu, false); }

void SaveFullRow(BOOL enable) { SaveOption(kFullRow, enable); }
bool ReadFullRow() { return ReadOption(kFullRow, false); }

void SaveShowGrid(BOOL enable) { SaveOption(kShowGrid, enable); }
bool ReadShowGrid(){ return ReadOption(kShowGrid, false); }

void SaveAlternativeSelection(BOOL enable) { SaveOption(kAlternativeSelection, enable); }
bool ReadAlternativeSelection(){ return ReadOption(kAlternativeSelection, false); }

void SaveSingleClick(BOOL enable) { SaveOption(kSingleClick, enable); }
bool ReadSingleClick(){ return ReadOption(kSingleClick, false); }

/*
void SaveUnderline(bool enable) { SaveOption(kUnderline, enable); }
bool ReadUnderline(){ return ReadOption(kUnderline, false); }
*/

// void SaveLockMemoryAdd(bool enable) { SaveLmOption(kLockMemoryAdd, enable); }
// bool ReadLockMemoryAdd() { return ReadLmOption(kLockMemoryAdd, true); }

void SaveLockMemoryEnable(BOOL enable) { Save7ZipOption(kLargePagesEnable, enable); }
bool ReadLockMemoryEnable() { return Read7ZipOption(kLargePagesEnable, false); }

static CString GetFlatViewName(/*UINT32 panelIndex*/)
{
  //TCHAR panelString[16];
  //ConvertUInt32ToString(panelIndex, panelString);
  return (CString)kFlatViewName;// + panelString;
}

void SaveFlatView(/*UINT32 panelIndex,*/ BOOL enable) { SaveOption(GetFlatViewName(/*panelIndex*/), enable); }
bool ReadFlatView(/*UINT32 panelIndex*/) { return ReadOption(GetFlatViewName(/*panelIndex*/), FALSE); }

} //namespace Registry

} //namespace Common
